﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Faust.Andrew.MTG.Player
{
	/// <summary>
	/// Interaction logic for JoinGameConfigurationControl.xaml
	/// </summary>
	public partial class JoinGameConfigurationControl : UserControl
	{
        private bool _showHostInformation;
        public bool ShowHostInformation
        {
            get { return _showHostInformation; }
            set
            {
                SetHostInformationVisibility(value);
            }
        }

        public new string Name
        {
            get { return txtName.Text; }
        }

        public string DeckName
        {
            get
            {
                if (lbDeck.SelectedItem is string)
                {
                    return lbDeck.SelectedItem as string;
                }
                else
                {
                    return String.Empty;
                }
            }
        }

        public bool IsRejoin
        {
            get
            {
                return chkRejoin.IsChecked.GetValueOrDefault();
            }
        }
        

        public List<string> Decks
        {
            set { lbDeck.ItemsSource = value; }
        }

        public string Host
        {
            get { return txtHost.Text; }            
        }

        public int Port
        {
            get
            {
                int port;
                if (Int32.TryParse(txtPort.Text, out port))
                {
                    return port;
                }
                else
                {
                    return -1;
                }
            }
        }

        public bool Validate()
        {
            if (String.IsNullOrEmpty(Name))
            {
                MessageBox.Show("You must enter a name");
                return false;
            }

            if (!IsRejoin && String.IsNullOrEmpty(DeckName))
            {
                MessageBox.Show("You must select a deck");
                return false;
            }

            if (ShowHostInformation)
            {
                if (Port < 0)
                {
                    MessageBox.Show("You must enter a port between 0-65535");
                    return false;
                }

                if (String.IsNullOrEmpty(Host))
                {
                    MessageBox.Show("You must enter the host address");
                    return false;
                }
            }

            return true;
        }

        private void SetHostInformationVisibility(bool isVisible)
        {

            if (!IsLoaded) { return; }

            _showHostInformation = isVisible;
            Visibility visibility = Visibility.Visible;

            if (!isVisible)
            {
                visibility = Visibility.Collapsed;
            }

            lblPort.Visibility = visibility;
            txtPort.Visibility = visibility;
            lblHost.Visibility = visibility;
            txtHost.Visibility = visibility;
        }


		public JoinGameConfigurationControl()
		{
			this.InitializeComponent();
		}
	}
}